package com.xiaodu.swagger.controller;

import com.xiaodu.swagger.entity.OrderEntity;
import com.xiaodu.swagger.entity.ResultBean;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author: dutianjie
 * @date: 2021/3/29
 * @version: v1.0.0
 * @description: 描述
 * @email: dutianjie@thunisoft.com
 **/
@RestController
@RequestMapping(value = "/test")
@Api(tags = "test-订单操作")  // 标注为swagger的一个资源
public class OrderController {

    private static final Logger log = LoggerFactory.getLogger(OrderController.class);

    @ApiOperation(value = "订单查询", notes = "根据orderId查询订单信息", httpMethod = "GET") // 对请求的表述
    @ApiResponses({  // 对可能返回的结果response描述
            @ApiResponse(code = 200, message = "ok"),
            @ApiResponse(code = 500, message = "error")
    })
    @GetMapping("/order")
    public ResultBean<OrderEntity> getName(
            @ApiParam(name = "orderId", value = "订单编号", required = true) @RequestParam String orderId) throws Exception {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId(orderId);
        orderEntity.setCreateTime(new Date());
        orderEntity.setName("xiaomi 11");
        return ResultBean.success(orderEntity);
    }

    @ApiOperation(value = "添加订单", httpMethod = "POST")
    @PostMapping("/order")
    public ResultBean<Void> addOrder(@RequestBody OrderEntity orderEntity) throws Exception {
        log.warn("添加 order = {}", orderEntity);
        return ResultBean.success(null);
    }

    @ApiOperation(value = "更新订单", notes = "根据orderId更新订单信息并返回更新后的订单信息", httpMethod = "PUT")
    @PutMapping("/order")
    public ResultBean<OrderEntity> updateOrder(@RequestBody OrderEntity orderEntity) throws Exception {
        log.warn("update order = {}", orderEntity);
        return ResultBean.success(orderEntity);
    }

    @ApiOperation(value = "删除订单", httpMethod = "DELETE")
    @DeleteMapping("order/{orderId}")
    public ResultBean<Void> delOrder(@ApiParam(value = "订单编号")@PathVariable String orderId) {
        log.warn("delete orderId = {}", orderId);
        return ResultBean.success(null);
    }

    @ApiOperation(value = "test-ApiImplicitParam", httpMethod = "GET", response = String.class)
    @ApiImplicitParams({  // 对请求参数的描述；当请求参数不是一个可描述的时候使用 同@ApiParam 作用一样
            @ApiImplicitParam(name = "name", value = "名称", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", dataTypeClass = Integer.class, paramType = "query")
    })
    @GetMapping(value = "/str", consumes = MediaType.ALL_VALUE)
    public String getStr(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "name = " + request.getParameter("name") + ", age = " + request.getParameter("age");
    }

}
